//
//  TopCategoryShowView.m
//  LiteraryTune
//
//  Created by 神州四达 on 2018/1/16.
//  Copyright © 2018年 cxd. All rights reserved.
//

#import "TopCategoryShowView.h"

@interface TopCategoryShowView () 

@property (nonatomic, strong)UIScrollView *contentScrollView;
@property (nonatomic, strong)UIButton *moreBtn;
@property (nonatomic, strong)UIButton *currentBtn;
@property (nonatomic, strong)UIView *lineView;

@end

@implementation TopCategoryShowView

- (void)setTitleArray:(NSArray *)titleArray {
    _titleArray = titleArray;
    [self addSubview:self.contentScrollView];
    [self addSubview:self.moreBtn];
    [self createView];
}

- (void)createView {
    self.backgroundColor = [UIColor whiteColor];
    CGFloat btnW = (kScreenWidth - 40) / 5;
    self.contentScrollView.contentSize = CGSizeMake(btnW * _titleArray.count, self.height);
    for (int index = 0; index < _titleArray.count; index++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(btnW * index, 3, btnW, self.height - 6);
        [btn setTitle:_titleArray[index] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:15];
        [btn setTitleColor:MainDarkGrayColor forState:UIControlStateNormal];
        btn.backgroundColor = [UIColor clearColor];
        btn.tag = index;
        if (index == 0) {
            _currentBtn = btn;
            btn.selected = YES;
        }
        [btn addTarget:self action:@selector(btnClickAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentScrollView addSubview:btn];
    }
    [self.contentScrollView addSubview:self.lineView];
}

#pragma mark - event response
- (void)btnClickAction:(UIButton *)sender {
//    if (_currentBtn != sender) {
        _currentBtn.selected = NO;
        sender.selected = YES;
        _currentBtn = sender;
        _lineView.x = (kScreenWidth - 40) / 5 * _currentBtn.tag;
        if (self.block) {
            self.block(_currentBtn.titleLabel.text);
        }
//    }
}

- (void)moreBtnClickAction {
    if (_currentBtn.tag + 1 != _titleArray.count) {
        UIButton *nextBtn = (UIButton *)[self viewWithTag:_currentBtn.tag + 1];
        nextBtn.selected = YES;
        _currentBtn.selected = NO;
        _currentBtn = nextBtn;
        if (_currentBtn.tag / 5.0 >= 1) {
            [_contentScrollView setContentOffset:CGPointMake((kScreenWidth - 40) / 5 * (_currentBtn.tag - 4), 0)];
        }
        _lineView.x = (kScreenWidth - 40) / 5 * _currentBtn.tag;
        if (self.block) {
            self.block(_currentBtn.titleLabel.text);
        }
    }
}

#pragma mark - 懒加载
- (UIScrollView *)contentScrollView {
    if (!_contentScrollView) {
        _contentScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth - 40, self.height)];
        _contentScrollView.showsHorizontalScrollIndicator = NO;
        _contentScrollView.bounces = NO;
    }
    return _contentScrollView;
}

- (UIButton *)moreBtn {
    if (!_moreBtn) {
        _moreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _moreBtn.frame = CGRectMake(kScreenWidth - 40, 0, 40, self.height);
        [_moreBtn setImage:[UIImage imageNamed:@"arrow"] forState:UIControlStateNormal];
        [_moreBtn addTarget:self action:@selector(moreBtnClickAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _moreBtn;
}

- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [[UIView alloc] initWithFrame:CGRectMake(0, self.height - 3, (kScreenWidth - 40) / 6, 3)];
        _lineView.backgroundColor = MainGreenColor;
    }
    return _lineView;
}

@end
